package com.yihuan.controller;

import com.yihuan.entity.OrderProductEntity;
import com.yihuan.entity.ShoppingEntity;
import com.yihuan.global.MessageConstant;
import com.yihuan.service.OrderProductService;
import com.yihuan.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/4/24 18:01
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class OrderProductController {
    @Autowired
    OrderProductService orderProductService;
    @Autowired
    ShoppingService shoppingService;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    
    
    /**
     * 新增数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/insertOrderProductByOrderProductData")
    public Map<String, Object> insertOrderProductByOrderProductData(@RequestBody OrderProductEntity model, HttpSession session) {
        // model.setUserID((Integer) session.getAttribute("UserID_4"));
        ShoppingEntity shoppingEntity = new ShoppingEntity();
        shoppingEntity.setShoppingState(2);
        shoppingEntity.setShoppingID(model.getRecProductID());
        List<ShoppingEntity> findShoppingByShoppingID = shoppingService.findShoppingByShoppingID(shoppingEntity);
        shoppingService.updateShoppingByShoppingDeleteState(shoppingEntity);
        //设置时间的格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取时间的方法
        Date d = new Date();
        //将获取的时间转换成设置的时间格式进行存储
        model.setOrderProductDate(sf.format(d));
        //状态
        model.setOrderProductState(1);
        //订单编号
        model.setOrderID((int) session.getAttribute("orderID"));
        //废品编号
        model.setRecProductID(findShoppingByShoppingID.get(0).getRecProductID());
        int insertOrderProductByOrderProductData = orderProductService.insertOrderProductByOrderProductData(model);
        if (insertOrderProductByOrderProductData == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.ORDER_DETAIL_INSERT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.ORDER_DETAIL_INSERT_SUCCESS);
        return map;
    }
    
    /**
     * 修改订单状态
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateOrderProductByOrderProductNum")
    public Map<String, Object> updateOrderProductByOrderProductNum(@RequestBody OrderProductEntity model, HttpSession session) {
        int updateOrderProductByOrderProductNum = orderProductService.updateOrderProductByOrderProductNum(model);
        if (updateOrderProductByOrderProductNum == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.ORDER_DETAIL_UPDATE_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.ORDER_DETAIL_UPDATE_SUCCESS);
        return map;
    }
    
    /**
     * 查询对应公司的订单信息
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findOrderProductByOrderProductData")
    public Map<String, Object> findOrderProductByOrderProductData(@RequestBody OrderProductEntity model, HttpSession session) {
        List<OrderProductEntity> findOrderProductByOrderProductData = orderProductService.findOrderProductByOrderProductData(model);
        if (findOrderProductByOrderProductData.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.ORDER_UPDATE_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("orderProduct_list", findOrderProductByOrderProductData);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
    
}
